<?php
/**
 * Bito functions and definitions.
 *
 * Sets up the theme and provides some helper functions, which are used
 * in the theme as custom template tags. Others are attached to action and
 * filter hooks in WordPress to change core functionality.
 *
 * When using a child theme (see http://codex.wordpress.org/Theme_Development and
 * http://codex.wordpress.org/Child_Themes), you can override certain functions
 * (those wrapped in a function_exists() call) by defining them first in your child theme's
 * functions.php file. The child theme's functions.php file is included before the parent
 * theme's file, so the child theme functions would be used.
 *
 * Functions that are not pluggable (not wrapped in function_exists()) are instead attached
 * to a filter or action hook.
 *
 * For more information on hooks, actions, and filters, see http://codex.wordpress.org/Plugin_API.
 *
 * @package WordPress
 * @subpackage Bito
 * @since Bito 1.0
 */

/**
 * Sets up the content width value based on the theme's design and stylesheet.
 */
if ( ! isset( $content_width ) )
	$content_width = 625;

/**
 * Sets up theme defaults and registers the various WordPress features that
 * Bito supports.
 *
 *
 * @since  Bito 2.0
 */
function bito_setup() {
	// Add default posts and comments RSS feed links to head
	add_theme_support( 'automatic-feed-links' );

	// This theme styles the visual editor with editor-style.css to match the theme style.
	add_editor_style();

	/*
	 * Makes Bito available for translation.
	 *
	 * Translations can be added to the /languages/ directory.
	 * If you're building a theme based on Bito, use a find and replace
	 * to change 'bito' to the name of your theme in all the template files.
	 * See: http://codex.wordpress.org/Function_Reference/load_theme_textdomain
	 */
	load_theme_textdomain( 'bito', get_template_directory() . '/languages' );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menu( 'primary', __( 'Primary Menu', 'bito' ) );

	// This theme uses a custom image size for featured images, displayed on "standard" posts.
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 624, 9999 ); // Unlimited height, soft crop

	// This theme allows users to set a custom background.
	add_theme_support( 'custom-background', array(
		// Let WordPress know what our default background color is.
		'default-color' => 'E0E0E0',
	) );
	// The custom header business starts here.
	add_theme_support( 'custom-header' );
}
add_action( 'init', 'bito_setup' );

/**
 * Registers our main widget area and the front page widget areas.
 *
 * @since Bito 2.0
 */
function bito_widgets_init() {
	register_sidebar( array(
		'name' => __( 'Main Sidebar', 'bito' ),
		'id' => 'sidebar-1',
		'before_widget' => '<li>',
		'after_widget' => '</li>',
		'before_title' => '<h2>',
		'after_title' => '</h2>',
	) );
}
add_action( 'widgets_init', 'bito_widgets_init' );

/**
 * get containt layout.
 *
 * @since Bito 2.0
 */
function bito_col_class() {
	$col_class = ' class="doublecol"';
	if ( !is_dynamic_sidebar() 
		|| (is_page() 
			&& !is_page_template('archives.php')
			&& !is_page_template('tags.php')) )
	$col_class = ' class="singlecol"';
	return $col_class;
}

/**
 * This function is based on WP's built-in get_archives()
 * It outputs the following:
 *
 * <table>
 * <tr>
 *     <th></th>
 *     <th>Jan</th>
 *     [..]
 * </tr>
 * <tr>
 *     <th><a href="link">year</a></td>
 *     <td><a href="link">month post count</a></td>
 *     [..]
 * </tr>
 * [..]
 * </table>
 * @since Bito 1.0
 */
function bito_archive()
{
	global $month, $wpdb;
	$arcresults = $wpdb->get_results('SELECT DISTINCT YEAR(post_date) AS year FROM ' 
		.$wpdb->posts 
		." WHERE post_status='publish' AND post_type='post' AND post_password='' "
		."ORDER BY post_date DESC ");

	if ($arcresults) {
		echo '<table width="100%">', "\n";
		echo '<tr>', "\n";
		echo '<td>&nbsp;</td>',"\n";
		echo '<th>Jan</th>',"\n";
		echo '<th>Feb</th>',"\n";
		echo '<th>Mar</th>',"\n";
		echo '<th>Apr</th>',"\n";
		echo '<th>May</th>',"\n";
		echo '<th>Jun</th>',"\n";
		echo '<th>Jul</th>',"\n";
		echo '<th>Aug</th>',"\n";
		echo '<th>Sep</th>',"\n";
		echo '<th>Oct</th>',"\n";
		echo '<th>Nov</th>',"\n";
		echo '<th>Dec</th>',"\n";
		echo '</tr>', "\n";
		foreach ($arcresults as $arcresult) {
			echo '<tr>', "\n";
			$yearurl  = get_year_link($arcresult->year);
			$tdArray = array ("<th>" . get_archives_link($yearurl, $arcresult->year, '') . "</th>") ;

			for ($n=1;$n<13;$n++) {
				$tdArray[]= '<td align="center">&nbsp;</td>';
			}
			$arcresults2 = $wpdb->get_results('SELECT DISTINCT MONTH(post_date) AS month, count(ID) as monthposts FROM ' 
				.$wpdb->posts 
				." WHERE YEAR(post_date)='$arcresult->year' AND post_status='publish' AND post_type='post' AND post_password='' "
				."GROUP BY MONTH(post_date) ORDER BY post_date DESC");
			foreach ($arcresults2 as $arcresult2) {
				$monthurl  = get_month_link($arcresult->year, $arcresult2->month);
				$tdArray[$arcresult2->month]='<td align="center">'. get_archives_link($monthurl, $arcresult2->monthposts, '') . '</td>';
			}

			for ($n=0;$n<13;$n++) {
				echo $tdArray[$n],"\n";
			}
			echo '</tr>', "\n";
		}
		echo '</table>', "\n";
	}
}

?>
